<?php

require_once(TANGRA_MAIN_DIR.'interfaces/i_db_storable.class.php');


require_once('rss_channel.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/rss/rss_channel_dbc.class.php');


class RSS_Channel_DBC extends RSS_Channel implements I_DB_Storable {
	public function save(DB_Connection $dbc) {
		$ret = false;

		if ($this->get_id()) {
			$ret = $this->update($dbc);
		} else {
			$ret = $this->insert($dbc);
			$this->set_id($ret);

		}

		return $ret;
	}


	public function load_by_id(DB_Connection $dbc, $id) {
		$ret = false;

		if (tangra_is_int($id)) {
			$sql = "select title, ".
							"link, ".
							"description, ".
							"language, ".
							"copyright, ".
							"managing_editor, ".
							"web_master, ".
							"ttl, ".
							"disabled ".
						"from rss_channels ".
						"where id = $id";
			$rez = $dbc->execute($sql);

			if (!$rez->is_eof()) {
				$rez_obj = $rez->fetch_object();

				$this->set_id($id);
				$this->set_title(stripslashes($rez_obj->TITLE));
				$this->set_link(stripslashes($rez_obj->LINK));
				$this->set_description(stripslashes($rez_obj->DESCRIPTION));
				$this->set_copyright(stripslashes($rez_obj->COPYRIGHT));
				$this->set_managing_editor(stripslashes($rez_obj->MANAGING_EDITOR));
				$this->set_web_master(stripslashes($rez_obj->WEB_MASTER));
				$this->set_ttl($rez_obj->TTL);
				$this->set_disabled($rez_obj->DISABLED);

				$ret = $id;
			}
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id('rss_channels_seq');

		$title = addslashes($this->get_title());
		$link = addslashes($this->get_link());
		$description = addslashes($this->get_description());
		$language = addslashes($this->get_language());
		$copyright = addslashes($this->get_copyright());
		$managing_editor = addslashes($this->get_managing_editor());
		$web_master = addslashes($this->get_web_master());
		$ttl = $this->get_ttl();
		$disabled = $this->get_disabled() ? 1 : 0;

		if ($id) {
			$sql = "insert into rss_channels (id, ".
											"title, ".
											"link, ".
											"description, ".
											"language, ".
											"copyright, ".
											"managing_editor, ".
											"web_master, ".
											"ttl, ".
											"disabled ".
									      ") ".
								"values ".
									      "($id,".
									      "'$title', ".
									      "'$link', ".
									      "'$description', ".
									      "'$language', ".
									      "'$copyright', ".
									      "'$managing_editor', ".
									      "'$web_master', ".
									      "$ttl, ".
									      "$disabled ".
									      ")";
			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception('ID not generated - rss_channels_seq');
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$id = $this->get_id();

		$title = addslashes($this->get_title());
		$link = addslashes($this->get_link());
		$description = addslashes($this->get_description());
		$language = addslashes($this->get_language());
		$copyright = addslashes($this->get_copyright());
		$managing_editor = addslashes($this->get_managing_editor());
		$web_master = addslashes($this->get_web_master());
		$ttl = $this->get_ttl();
		$disabled = $this->get_disabled() ? 1 : 0;

		$sql = "update rss_channels set ".
						"title = '$title', ".
						"link = '$link', ".
						"description = '$description', ".
						"language = '$language', ".
						"copyright = '$copyright', ".
						"managing_editor = '$managing_editor', ".
						"web_master = '$web_master', ".
						"ttl = $ttl, ".
						"disabled = $disabled ".
					"where id = ".$id;

		$dbc->execute($sql);
		$ret = $id;


		return $ret;
	}


	public static function get_sql_for_grid() {
		$sql = "select id, title, disabled from rss_channels order by title asc";

		return $sql;
	}


	public static function get_sql_count_for_grid() {
		$sql = "select count(id) as total_rows from rss_channels ";

		return $sql;
	}


	public static function delete(DB_Connection $dbc, $id) {
		tangra_if_not_int_throw_e($id);

		$sql = "delete from rss_channels where id = $id";
		$dbc->execute($sql);
	}


	public static function load_rss_channels_ol_map(DB_Connection $dbc) {
		$sql = "select id, title from rss_channels order by title asc";
		$rez = $dbc->execute($sql);

		$ret = array();
		$ids = array();

		while($rez_obj = $rez->fetch_object()) {
			array_push($ids, $rez_obj->ID);
			$ret['ol_map'][$rez_obj->ID] = $rez_obj->TITLE;
		}

		if ($ids) {
			$ret['ids'] = $ids;
		}


		return $ret;
	}
}